/**
 * 湖南江河机电自动化设备股份有限公司 版权所有
 * Copyright @2016-2020 All Rights Reserved
 */
package com.hnjme.cnec.mdm.tasks;

import com.hnjme.cnec.mdm.ws.MdmDispatchManager;
import com.hnjme.cnec.mdm.ws.OrganizationResult;
import com.hnjme.cnec.mdm.ws.SearchCondition;
import com.hnjme.cnec.mdm.ws.StaffResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import reactor.core.Reactor;
import reactor.event.Event;

import java.util.List;

/**
 * @author outta
 * @Since 1.0 2018/8/29
 */
@Slf4j
@Component
public class MasterDataSyncTask {
    @Autowired
    private MdmDispatchManager wsClient;

    @Autowired
    @Qualifier("processor")
    private Reactor masterDataProcessor;

    /**
     * 半小时同步一次
     */
	@Scheduled(cron = "0 0/2 * * * ?")
    public void syncOrganizations() {
        log.info("==> \t开始同步主数据系统的机构信息......");

        SearchCondition condition = new SearchCondition();
        condition.setSysCode("QHSE");
        condition.setOrgCclx(null);
        condition.setOrgStlx(null);
        //调用查询方法
        List<OrganizationResult> organizations = wsClient.getUnitInfo(condition);
        log.info("==> \t本次共同步到[{}]条组织机构数据", null == organizations ? 0 : organizations.size());
        if(null != organizations && organizations.size() > 0){
			masterDataProcessor.notify("organizationsProcess", Event.wrap(organizations));
		}

    }

    @Scheduled(cron = "0 0/2 * * * ?")
    public void syncFIOrganizations() {
        log.info("==> \t开始同步主数据系统的财务组织机构信息......");

        SearchCondition condition = new SearchCondition();
        condition.setSysCode("QHSE");
        condition.setOrgCclx(null);
        condition.setOrgStlx(null);
        //调用查询方法
        List<OrganizationResult> organizations = wsClient.getFIUnitInfo(condition);
        log.info("==> \t本次共同步到[{}]条财务组织机构数据", null == organizations ? 0 : organizations.size());
        if(null != organizations && organizations.size() > 0){
            masterDataProcessor.notify("fiOrganizationsProcess", Event.wrap(organizations));
        }
    }

    @Scheduled(cron = "0 0/2 * * * ?")
    public void syncStaffs() {
        log.info("==> \t开始同步主数据系统的员工信息......");

        SearchCondition condition = new SearchCondition();
        condition.setSysCode("QHSE");
        condition.setOrgCclx(null);
        condition.setOrgStlx(null);
        //调用查询方法
        List<StaffResult> staffs = wsClient.getStaffInfo(condition);
        log.info("==> \t本次共同步到[{}]条员工数据", null == staffs ? 0 : staffs.size());
        if(null != staffs && staffs.size() > 0){
            masterDataProcessor.notify("staffsProcess", Event.wrap(staffs));
        }
    }
}
